import 'dart:convert';

import 'package:Flutter_Demo/core/model/category_model.dart';
import 'package:Flutter_Demo/core/model/meal_model.dart';
import 'package:flutter/services.dart';

class MYJsonParse {
  
  static Future<List<MYCategoryModel>> getCategoryData() async {
    // 1、加载json文件
    final jsonString = await rootBundle.loadString("assets/json/category.json");
    // 2、将jsonString转化为Map/List
    final result = json.decode(jsonString);
    // 3、将Map中的内容转化成一个一个对象
    final resultList = result["category"];
    List<MYCategoryModel> categories = [];

    for(var json in resultList) {
      categories.add(MYCategoryModel.fromJson(json));
    }
    return categories;
  }

  static Future<List<MYMealModel>> getMealData() async {
    // 1、加载json文件
    final jsonString = await rootBundle.loadString("assets/json/meal.json");
    // 2、将jsonString转化为Map/List
    final result = json.decode(jsonString);
    // 3、将Map中的内容转化成一个一个对象
    final resultList = result["meal"];
    List<MYMealModel> meals = [];

    for(var json in resultList) {
      meals.add(MYMealModel.fromJson(json));
    }
    return meals;
  }
  
}
